<?php


namespace app\api\controller\v1;


use app\api\model\ServiceAreaModel;
use app\common\controller\UserBaseController;

class SvcareaController extends UserBaseController
{

    function infoAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'id' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $info = (new ServiceAreaModel())->info($this->city_id, $this->argv['id']);
        $this->resp['info'] = $info;

        return $this->ret_resp();
    }

    function listAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'city_id' => 'string',
            'state' => 'optint',
            'sk' => 'optstring',
            'pno' => 'int',
            'psize' => 'int',
            'flush' => ['optint', 0],
        );

        $this->check_params();
        $this->check_token();

        $list = (new ServiceAreaModel())->get_list($this->city_id, $this->argv, $this->argv['pos'], $this->argv['limit']);
        $this->resp['list'] = $list;
        $this->resp['total'] = hisql::query_count();

        return $this->ret_resp();
    }

    function addAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'city_id' => 'string',
            'id' => 'optstring',
            'name' => 'string',
            'remark' => 'string',
            'coordinates' => 'polygon',
            'state' => 'int',
            'price_rule' => 'optarray',
        );

        $this->check_params();
        $this->check_token();

        $info = [
            'city_id' => $this->city_id,
            'name' => $this->argv['name'],
            'remark' => $this->argv['remark'],
            'coordinates' => $this->argv['coordinates'],
            'state' => $this->argv['state'],
        ];
        if (isset($this->argv['price_rule'])) {
            $info['price_rule'] = $this->argv['price_rule'];
        }

        (new ServiceAreaModel())->add($info);
        $this->resp['info'] = $info;

        return $this->ret_resp();
    }

    function updateAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'id' => 'string',
            'name' => 'optstring',
            'remark' => 'optstring',
            'state' => 'optoptint',
            'coordinates' => 'optpolygon',
            'price_rule' => 'optarray',
        );

        $this->check_params();
        $this->check_token();
        $info = (new ServiceAreaModel())->info($this->city_id, $this->argv['id']);
        if (empty($info)) {
            $this->throw_myexception(X_ERROR, "查询服务区信息失败！");
        }

        $data = [];
        foreach ($this->argv as $k => $v) {
            if (isset($info[$k]) && $this->argv[$k] == $v) {
                continue;
            }
            $data[$k] = $v;
        }

        $info = (new ServiceAreaModel())->update($this->city_id, $this->argv['id'], $this->argv);
        $this->resp['info'] = $info;

        return $this->ret_resp();
    }

    function is_inAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'city_id' => 'string',
            'coordinate' => 'array',
        );
        $this->check_params();
        //$this->check_token();

        $result = (new ServiceAreaModel())->is_in(null, $this->argv['coordinate']);
        $this->resp['list'] = $result;

        return $this->ret_resp();
    }

    function delAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'id' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $info = (new ServiceAreaModel())->delete_serva($this->city_id, $this->argv['id']);
        $this->resp['info'] = $info;

        return $this->ret_resp();
    }

    function on_the_edgeAction()
    {
        $this->paramlist = [
            'coordinate' => 'array',
        ];

        //$this->check_params();


        $a = [119.279379, 26.060798];
        $polygon = [[119.277817, 26.060704], [119.279325, 26.061721], [119.280859, 26.060574], [119.27925, 26.059519], [119.277817, 26.060704]];

        $this->resp['x'] = (new \geovector())->point_polygon_short_distance($a, $polygon);

        return $this->ret_resp();
    }

}
